package ModelImplementation

import "time"

type UnionAffiliation struct {
	itsMemberId int
	itsDues float64
	itsServiceCharges map[time.Time]ServiceCharge
}

func (u UnionAffiliation) newUnionAffiliation(memberId int, dues float64)  {
	u.itsMemberId = memberId
	u.itsDues = dues
}

func (u UnionAffiliation) GetServiceCharges(date time.Time) ServiceCharge {
	return u.itsServiceCharges[date]
}

func (u UnionAffiliation) AddServiceCharge(date time.Time, amount float64)  {
	sc := ServiceCharge.newServiceCharge(date,amount)
	u.itsServiceCharges[date] = sc
}

func (u UnionAffiliation) GetDues() float64 {
	return u.itsDues
}

func (u UnionAffiliation) GetMemberId() int {
	return u.itsMemberId
}

func NumberOfFridaysInPayPeriod(payPeriodStart time.Time, payPeriodEnd time.Time) int {
	fridays := 0

	for date := payPeriodStart; date.After(payPeriodEnd.AddDate(0,0,1)); date = date.AddDate(0,0,1) {
		if date.Weekday() == time.Friday {
			fridays++
		}
	}

	return fridays
}

func (u UnionAffiliation) CalculateDeductions(pc Model.Paycheck) float64 {
	totalServiceCharge := 0.0
	totalDues := 0.0

	for key, value := range u.itsServiceCharges {
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()

		if key.After(dt1) && key.Before(dt2) {
			totalServiceCharge += value.GetAmount()
		}
	}

	fridays := NumberOfFridaysInPayPeriod(pc.GetPayPeriodStartDate(),pc.GetPayPeriodEndDate())
	//noinspection GoBinaryAndUnaryExpressionTypesCompatibility
	totalDues = float64(fridays * u.itsDues)
	return totalDues + totalServiceCharge
}
